package com.newlink.building.userinfo.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.utils.ToastUtils
import com.newlink.building.common_base.utils.logDebug
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityAccountInformationChangePhoneInputCodeBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_AccountInformationChangePhoneInputCodePresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_AccountInformationChangePhoneInputCodeContract

/**
 * @author wangyang
 * @date 2022/10/28
 * @description 我的——账户信息——更换手机号页面（第三步：输入验证码）
 */
class Module_UserInfo_AccountInformationChangePhoneInputCodeActivityNL :
    NL_BaseMvpActivity<Module_UserInfo_AccountInformationChangePhoneInputCodeContract.View, Module_UserInfo_AccountInformationChangePhoneInputCodeContract.Presenter>(),
    Module_UserInfo_AccountInformationChangePhoneInputCodeContract.View {

    private lateinit var bindPhone: String
    private lateinit var bindArea: String

    private lateinit var mBinding: ActivityAccountInformationChangePhoneInputCodeBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityAccountInformationChangePhoneInputCodeBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
        bindPhone = intent.getStringExtra("bind_phone")!!
        bindArea = intent.getStringExtra("bind_area")!!

        if (TextUtils.isEmpty(bindPhone) || TextUtils.isEmpty(bindArea)) {
            ToastUtils.showTextToast(this, "未获取到手机号或地区码")
            finish()
        }
    }

    override fun initView() {
        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.account_information_change_phone_title)

        mBinding.tvDescribe.text =
            getString(R.string.account_information_bind_phone_input_code_describe, "17801063494")

        mBinding.bindVerifyView.listener = { s, b ->
            logDebug<Module_UserInfo_AccountInformationChangePhoneInputCodeActivityNL>("输入验证码: $s --- $b")
            if (s.length == 6) {
                showLoading()
                mPresenter?.sendBindPhoneCode(accessToken, bindArea, bindPhone, userId, s)
            }
        }
    }

    override fun createPresenter(): Module_UserInfo_AccountInformationChangePhoneInputCodeContract.Presenter =
        Module_UserInfo_AccountInformationChangePhoneInputCodePresenter()

    override fun showData() {}

    override fun bindPhoneSuccess() {
        hideLoading()
        ToastUtils.showTextToast(this, "绑定成功")
        val intent = Intent(this, Module_UserInfo_AccountInformationActivityNL::class.java)
        intent.putExtra("bindResult", 1)
        startActivity(intent)
        finish()
    }

    override fun bindPhoneFailed() {
        hideLoading()
        ToastUtils.showTextToast(this, "绑定失败")
        finish()
    }
}